package com.jerry.utils.str;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class SpringBootEnv implements CommandLineRunner, EnvironmentAware {
	
	
	private static final Logger log = LoggerFactory.getLogger(SpringBootEnv.class);


    private Environment environment;
    private static Environment env;

    @Override
    public void run(String... args) throws Exception {
      
        SpringBootEnv.env=environment;  //获取环境变量并赋值到静态变量，以便静态方法调用。
        
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
    
    public static String getKey(String key) {
		//通过实现CommandLineRunner, EnvironmentAware接口，获取到spring环境变量
    	
    	
    	if (getEnv().getProperty(key) != null) {
    	return env.getProperty(key);
    	}else {
    		return "";
    	}
	}
    
    public static Environment getEnv() {
		//通过实现CommandLineRunner, EnvironmentAware接口，获取到spring环境变量
    	if(env!=null) {
    		return env;
    	}else {
    		SpringApplication.run(SpringBootEnv.class, "");
        	return env;
    	}
    	
	}
    
    public static void main(String [] args) {
    	//log.warn("aaaabbb"+LocalEnv.getKey("zbx.user"));
    }

	
}
